//////////////////////////////////////////////////////////////////////////
///	\file:	MService.cpp
///	Copyright (MingYou) 2009 - All Rights Reserved
///	Author:	Robert.Ma
///	Date:	[2009/05/14]
///	Description:
///
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"

#include <msxml2.h>
#include <comutil.h>

#ifdef _DEBUG
#pragma comment(lib, "comsuppwd.lib")
#else
#pragma comment(lib, "comsuppw.lib")
#endif

#include "MService.h"
#include "w3c.h"
#include "CETool.h"

#include "SocketManager.h"
#include "STLogFile.h"

#define CMWAP_PROXY _T("10.0.0.172:80")

namespace MService
{
	LPCTSTR GetGBWhiteList()
	{
		static CStringW strGBWhiteList;

		if ( strGBWhiteList.IsEmpty() )
		{
			W3Client client;

			if ( client.Connect(_T("http://g.10086.cn/"),
								NULL, NULL, __W3_DEFAULT_AGENT, CMWAP_PROXY) )
			{
				if ( client.Request(_T("/home/interface/AutoRegLogin.php?app=qbdx"), W3Client::reqGet) )
				{
					char byBuf[1024];
					ZeroMemory(byBuf, 1024);

					if ( 0 < client.Response((unsigned char*)(byBuf), 1024) )
					{
						CStringA strRevA(byBuf);
						strGBWhiteList = CETool::UTF8toUTF16(strRevA);
					}
				}

				client.Close();

				STLOG_WRITE(_T("%s \n"), strGBWhiteList);
			}
		}



		return strGBWhiteList;
	}

	LPCTSTR Get139WhiteList()
	{
		static CStringW str139WhiteList;

		if ( str139WhiteList.IsEmpty() )
		{
			W3Client client;
			client.InitializePostArguments();

			if ( client.Connect(_T("http://wap1.m.139.com/"),
								NULL, NULL, __W3_DEFAULT_AGENT, CMWAP_PROXY) )
			{
				client.AddPostArgument("api_key", "29e6ab7366559f9d15661f263815d2e4", false);
				client.AddPostArgument("secret", "22e0ddf658a6c0c6a8e8b199fad52e1c", false);

				if ( client.Request(_T("/rest.do?do=Users.login"), W3Client::reqPost) )
				{
					char byBuf[1024];
					ZeroMemory(byBuf, 1024);

					if ( 0 < client.Response((unsigned char*)(byBuf), 1024) )
					{
						CStringA strRevA(byBuf);
						str139WhiteList = CETool::UTF8toUTF16(strRevA);
					}
				}

				client.Close();
			}
		}

		return str139WhiteList;
	}

#pragma warning (disable : 4310)

	BOOL QueryGBWhiteList(LPCTSTR lpstrKey, CString& strValue)
	{
		BOOL bResult = FALSE;

		LPCTSTR lpXml = GetGBWhiteList();

		if ( NULL != lpXml && 0 < _tcslen(lpXml) )
		{
			CLSID clsid;
			IXMLDOMDocument* pDOM = NULL;

			::CLSIDFromProgID(TEXT("Msxml2.DOMDocument"), &clsid);
			HRESULT hResult = ::CoCreateInstance(clsid,
												 NULL,
												 CLSCTX_INPROC_SERVER,
												 IID_IXMLDOMDocument,
												 (LPVOID*)(&pDOM));

			if ( SUCCEEDED(hResult) )
			{
				VARIANT_BOOL bSuccess = VARIANT_FALSE;

				hResult = pDOM->loadXML(_bstr_t(lpXml), &bSuccess);

				if ( VARIANT_TRUE == bSuccess )
				{
					IXMLDOMElement* pRoot = NULL;
					hResult = pDOM->get_documentElement(&pRoot);

					if ( SUCCEEDED(hResult) && NULL != pRoot )
					{
						IXMLDOMNode* pNode = NULL;
						hResult = pRoot->selectSingleNode(_bstr_t(lpstrKey), &pNode);

						if ( SUCCEEDED(hResult) )
						{
							BSTR bstrText = NULL;
							hResult = pNode->get_text(&bstrText);

							if ( SUCCEEDED(hResult) )
							{
								strValue = (LPCTSTR)(_bstr_t(bstrText, true));

								::SysFreeString(bstrText);

								bResult = TRUE;
							}
						}

						pNode->Release();
					}

					pRoot->Release();
				}

				pDOM->Release();
			}
		}

		return bResult;
	}

	BOOL Query139WhiteList(LPCTSTR /*lpstrKey*/, CString& strValue)
	{
		BOOL bResult = FALSE;

		LPCTSTR lpXml = Get139WhiteList();

		strValue = lpXml;

		//if ( NULL != lpXml && 0 < _tcslen(lpXml) )
		//{
		//	CLSID clsid;
		//	IXMLDOMDocument* pDOM = NULL;

		//	::CLSIDFromProgID(TEXT("Msxml2.DOMDocument"), &clsid);
		//	HRESULT hResult = ::CoCreateInstance(clsid,
		//	                                     NULL,
		//	                                     CLSCTX_INPROC_SERVER,
		//	                                     IID_IXMLDOMDocument,
		//	                                     (LPVOID*)(&pDOM));

		//	if ( SUCCEEDED(hResult) )
		//	{
		//		VARIANT_BOOL bSuccess = VARIANT_FALSE;

		//		hResult = pDOM->loadXML(_bstr_t(lpXml), &bSuccess);

		//		if ( VARIANT_TRUE == bSuccess )
		//		{
		//			IXMLDOMElement* pRoot = NULL;
		//			hResult = pDOM->get_documentElement(&pRoot);

		//			if ( SUCCEEDED(hResult) && NULL != pRoot )
		//			{
		//				IXMLDOMNode* pNode = NULL;
		//				hResult = pRoot->selectSingleNode(_bstr_t(lpstrKey), &pNode);

		//				if ( SUCCEEDED(hResult) )
		//				{
		//					BSTR bstrText = NULL;
		//					hResult = pNode->get_text(&bstrText);

		//					if ( SUCCEEDED(hResult) )
		//					{
		//						strValue = (LPCTSTR)(_bstr_t(bstrText, true));

		//						::SysFreeString(bstrText);

		//						bResult = TRUE;
		//					}
		//				}

		//				pNode->Release();
		//			}

		//			pRoot->Release();
		//		}

		//		pDOM->Release();
		//	}
		//}

		return bResult;
	}
#pragma warning (default : 4310)

	ISocketManager* CreateSocketManager(INetCallback* pCallback)
	{
		ISocketManager*	pResult		= NULL;
		CSocketManager*	pManager	= new CSocketManager();

		if ( NULL != pManager && pManager->Initialize(pCallback) )
		{
			pResult = pManager;
		}
		else
		{
			delete pManager;
		}

		return pResult;
	}

	BOOL FreeSocketManager(ISocketManager* pManager)
	{
		BOOL bResult = FALSE;
		CSocketManager* pFree = dynamic_cast<CSocketManager*>(pManager);

		if ( NULL != pFree )
		{
			delete pFree;
			bResult = TRUE;
		}

		return bResult;
	}
}
